﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FRPages
{
    public partial class FormConfirm : Form
    {
        private List<Image> listImages = new List<Image>();

        private List<string> _sources = new List<string>();

        public List<Button> listPageButtons = new List<Button>();
 
        private int _focusIndex = 0;
        public int FocusIndex
        {
            get { return _focusIndex; }
        }

        public FormConfirm(List<string> list,string btnCancelText,string btnRetakeText,string btnOkText,string tips)
        {
            _sources.AddRange(list);
            InitializeComponent();
            listPageButtons.Add(buttonFirst);
            listPageButtons.Add(buttonSecond);
            listPageButtons.Add(buttonThird);
            listPageButtons.Add(buttonFouth);

            lblTitle.Text = tips;
            buttonCancell.Text = btnCancelText;
            buttonOK.Text = btnOkText;
            buttonRetry.Text = btnRetakeText;
        }

        public void LoadPagePhotos(List<string> list)
        {
            listImages.Clear();
            for (int i = 0; i < list.Count; i++)
            {
                if (File.Exists(list[i]))
                {
                    listImages.Add(Image.FromFile(list[i]));
                }                
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void ShowFocusPageButton()
        {
            for (int i = 0; i < listPageButtons.Count; i++)
            {
                if (i == _focusIndex)
                {
                    listPageButtons[i].ForeColor = Color.Red;
                    listPageButtons[i].FlatAppearance.BorderColor = Color.Red;
                    listPageButtons[i].FlatAppearance.BorderSize = 5;                   
                }
                else
                {
                    listPageButtons[i].ForeColor = Color.DarkKhaki;
                    listPageButtons[i].FlatAppearance.BorderColor = Color.DarkKhaki;
                    listPageButtons[i].FlatAppearance.BorderSize = 1;                 
                }
            }
        }

        private void ShowFocusImage()
        {
            pictureBox1.Image = listImages[_focusIndex];
            EnableButtons();
            ShowFocusPageButton();
        }

        private void EnableButtons()
        {
            if (_focusIndex == 0)
            {
                buttonBack.Enabled = false;
            }
            if (_focusIndex > 0)
            {
                buttonBack.Enabled = true;
            }

            if (_focusIndex == _sources.Count - 1)
            {
                buttonNext.Enabled = false;
            }
            else
            {
                buttonNext.Enabled = true;
            }
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            if (_focusIndex < _sources.Count - 2)
            {
                _focusIndex++;
                ShowFocusImage();
            }
            
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            if (_focusIndex > 0 && _focusIndex <= _sources.Count - 1)
            {
                _focusIndex--;               
            }
            ShowFocusImage();
        }

        private void FormConfirm_Load(object sender, EventArgs e)
        {
            LoadPagePhotos(_sources);
            ShowFocusImage();
        }

        private void buttonRetry_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Retry;
            this.Close();
        }

        private void buttonCancell_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void buttonFirst_Click(object sender, EventArgs e)
        {
            _focusIndex = 0;
            ShowFocusImage();
        }

        private void buttonSecond_Click(object sender, EventArgs e)
        {
            _focusIndex = 1;
            ShowFocusImage();
        }

        private void buttonThird_Click(object sender, EventArgs e)
        {
            _focusIndex = 2;
            ShowFocusImage();
        }

        private void buttonFouth_Click(object sender, EventArgs e)
        {
            _focusIndex = 3;
            ShowFocusImage();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            _focusIndex = 4;
            ShowFocusImage();
        }
    }
}
